package com.revzone.demo001.PermissionModule.controller;

import com.revzone.demo001.PermissionModule.service.MenuService;
import com.revzone.demo001.entity.OtherEntity.R;
import com.revzone.demo001.entity.PermissionEntity.Menu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lsa
 * @since 2023-03-27
 */
@Api(tags = "Menu菜单接口", description = "用于Menu菜单的相关API")
@RestController
@RequestMapping("/menu")
public class MenuController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    MenuService menuService;
    /**
     * 拥有 user, admin 角色的用户可以访问下面的页面
     * 查询菜单
     */
    @ApiOperation(value = "用于根据ParentID查询Menu菜单父子集列表")
    @GetMapping(value="/getMenuList", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R getMenuList(@RequestParam(required = false)Integer parentId) {
        logger.info("欢迎来到菜单查询接口!");
        if (parentId == null) parentId=0;
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",menuService.getMenuList(parentId));
        return R.ok(map);
    }

    @ApiOperation(value = "用于根据id查询Menu菜单父子集列表")
    @GetMapping(value="/getMenuListById", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R getMenuListById(@RequestParam(required = false)Integer id) {
        logger.info("欢迎来到菜单查询接口!");
        if (id == null) id=0;
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",menuService.getMenuListById(id));
        return R.ok(map);
    }

    /**
     * 拥有 user, admin 角色的用户可以访问下面的页面
     * 添加菜单
     */
    @ApiOperation(value = "用于菜单新增接口")
    @PostMapping(value="/setMenuList", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R setMenuList(@RequestBody(required = false) Menu menu) {
        logger.info("欢迎来到菜单新增接口!");
        if (menu == null) menu=new Menu();
        return menuService.setMenuList(menu);
    }

    /**
     * 拥有 user, admin 角色的用户可以访问下面的页面
     * 修改菜单
     */
    @ApiOperation(value = "用于菜单修改接口")
    @PostMapping(value="/updateMenuList", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R updateMenuList(@RequestBody(required = false) Menu menu) {
        logger.info("欢迎来到菜单修改接口!");
        if (menu == null) menu=new Menu();
        return menuService.updateMenuList(menu);
    }

    /**
     * 拥有 user, admin 角色的用户可以访问下面的页面
     * 删除菜单
     * required 表示是否必须，默认为 true，必须。
     * defaultValue 可设置请求参数的默认值，如果设置了该值，required=true将失效，自动为false,如果没有传该参数，就使用默认值。
     * value 为接收url的参数名（相当于key值）。
     */
    @ApiOperation(value = "用于菜单删除接口")
    @GetMapping(value="/delMenuList", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R delMenuList(@RequestParam(required = false,defaultValue = "0",value = "id") Integer id) {
        logger.info("欢迎来到菜单删除接口!");
        return menuService.delMenuList(id);
    }

}
